package com.hzjq.core.util

import android.text.TextUtils
import com.hzjq.core.ErrorCode
import com.hzjq.core.bean.CapEntity
import com.hzjq.core.bean.ErrorCapEntity

class ErrorChecker {

    companion object {

        /**
         * 下传雷管错误检查
         */
        fun checkUnder(cap:CapEntity):ErrorCapEntity?{
            return checkMeetMiss(cap)
        }

        /**
         * 多接检查
         */
        fun checkMeet(cap:CapEntity):ErrorCapEntity?{
            if (TextUtils.equals("0", cap.status.toCharArray()[5].toString())) {
                val errorCapEntity = ErrorCapEntity()
                errorCapEntity.errorCode = 1
                errorCapEntity.error = ErrorCode.getScanError(1)
                errorCapEntity.cap = cap
                return errorCapEntity
            }
            return null
        }


        /**
         * 漏接接检查
         */
        fun checkMiss(cap:CapEntity):ErrorCapEntity?{
            if (TextUtils.equals("0", cap.status.toCharArray()[6].toString())) {
                val errorCapEntity = ErrorCapEntity()
                errorCapEntity.errorCode = 2
                errorCapEntity.error = ErrorCode.getScanError(2)
                errorCapEntity.cap = cap
                return errorCapEntity
            }
            return null
        }


        /**
         * 多接漏接检查
         */
        fun checkMeetMiss(cap:CapEntity):ErrorCapEntity?{
            val checkMeet = checkMeet(cap)
            if(checkMeet != null) return checkMeet
            return checkMiss(cap)
        }

        /**
         * 检查授权
         */
        fun checkAuth(cap:CapEntity):ErrorCapEntity?{
            val checkMeetMiss = checkMeetMiss(cap)
            if(checkMeetMiss == null){
                if (TextUtils.equals("0", cap.status.toCharArray()[1].toString())) {
                    val errorCapEntity = ErrorCapEntity()
                    errorCapEntity.errorCode = 7
                    errorCapEntity.error = ErrorCode.getScanError(7)
                    errorCapEntity.cap = cap
                    return errorCapEntity
                }
            }
            return checkMeetMiss
        }


        /**
         * 校时检查
         */
        fun checkTime(cap:CapEntity):ErrorCapEntity?{
            val checkMeetMiss = checkMeetMiss(cap)
            if(checkMeetMiss == null) {
                if (TextUtils.equals("0", cap.status.toCharArray()[2].toString())) {
                    val errorCapEntity = ErrorCapEntity()
                    errorCapEntity.errorCode = 9
                    errorCapEntity.error = ErrorCode.getScanError(9)
                    errorCapEntity.cap = cap
                    return errorCapEntity
                }
            }
            return checkMeetMiss
        }


        /**
         * 充电检查
         */
        fun checkCharge(cap:CapEntity):ErrorCapEntity?{
//            if (TextUtils.equals("0", cap.status.toCharArray()[4].toString())) {
//                val errorCapEntity = ErrorCapEntity()
//                errorCapEntity.errorCode = 10
//                errorCapEntity.error = ErrorCode.getScanError(10)
//                errorCapEntity.cap = cap
//                return errorCapEntity
//            }
            val checkMeetMiss = checkMeetMiss(cap)
            if(checkMeetMiss == null) {
                if (TextUtils.equals("0", cap.status.toCharArray()[0].toString())) {
                    val errorCapEntity = ErrorCapEntity()
                    errorCapEntity.errorCode = 12
                    errorCapEntity.error = ErrorCode.getScanError(12)
                    errorCapEntity.cap = cap
                    return errorCapEntity
                }
            }
            return checkMeetMiss
        }


        /**
         * 爆破检查
         */
        fun checkBlast(cap:CapEntity):ErrorCapEntity?{
            val checkUnder = checkUnder(cap)
            if(checkUnder != null) return checkUnder
            val checkAuth = checkAuth(cap)
            if(checkAuth != null) return checkAuth
            val checkTime = checkTime(cap)
            if(checkTime != null) return checkTime
            return checkCharge(cap)
        }
    }
}